package com.zmn.oms.dubbo.impl.quotation;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.model.dto.order.NewOrderExternalShopDTO;
import lombok.extern.slf4j.Slf4j;
import com.zmn.oms.model.dto.order.NewOrderDistributeMasterDTO;
import org.apache.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.dio.quotation.QuotationOrderWorkAddDIO;
import com.zmn.oms.dubbo.dto.zmn.order.NewOrderDRO;
import com.zmn.oms.dubbo.impl.zmn.order.OrderWorkBaseRemoteService;
import com.zmn.oms.dubbo.interfaces.quotation.QuotationOrderWorkRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.order.zmn.ZsNormalNewOrderDTO;
import com.zmn.oms.model.dto.product.OrderProductDTO;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2021/04/06 14:23
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class QuotationOrderWorkRemoteServiceImpl extends OrderWorkBaseRemoteService implements QuotationOrderWorkRemoteService  {

    private static final String TAG = "新计价器订单";

    @Override
    public ResponseDTO<NewOrderDRO> addQuotationOrder(QuotationOrderWorkAddDIO quotationOrderWorkAddDIO) {

        log.info("[{}] 添加计价器订单：NewShopOrderDIO={}", TAG, JSON.toJSONString(quotationOrderWorkAddDIO));

        if (StringUtils.isBlank(quotationOrderWorkAddDIO.getOperator())) {
            quotationOrderWorkAddDIO.setOperator("系统");
        }

        //基础信息
        ZsNormalNewOrderDTO dto = BeanMapper.map(quotationOrderWorkAddDIO, ZsNormalNewOrderDTO.class);
        dto.setQuotationType(quotationOrderWorkAddDIO.getOperatorType());
        dto.setIp(quotationOrderWorkAddDIO.getOperatorIP());
        BeanMapper.copy(quotationOrderWorkAddDIO.getOrderWorkBaseDIO(), dto);

        // 产品信息
        OrderProductDTO orderProductDTO = BeanMapper.map(quotationOrderWorkAddDIO.getOrderProductDIO(), OrderProductDTO.class);
        dto.setOrderProductList(Lists.newArrayList(orderProductDTO));
        // 用户信息
        BeanMapper.copy(quotationOrderWorkAddDIO.getUserDIO(), dto);
        dto.setUserName(StringUtils.defaultString(quotationOrderWorkAddDIO.getUserDIO().getContactName(), quotationOrderWorkAddDIO.getUserDIO().getUserName()));
        // 优惠信息
        if (Objects.nonNull(quotationOrderWorkAddDIO.getOrderAmountAndDiscountDIO())) {
            BeanMapper.copy(quotationOrderWorkAddDIO.getOrderAmountAndDiscountDIO(), dto);
            List<String> couponCodes = quotationOrderWorkAddDIO.getOrderAmountAndDiscountDIO().getCouponCodes();
            if (CollectionUtil.isNotNullOrEmpty(couponCodes)){
                dto.setDiscountItemCodes(couponCodes);
            }
        }
        // 扩展数据
        if (Objects.nonNull(quotationOrderWorkAddDIO.getExtraData())){
            BeanMapper.copy(quotationOrderWorkAddDIO.getExtraData(), dto);
            dto.setAutoDistribute(quotationOrderWorkAddDIO.getExtraData().getDutyMasterId());
        }
        // 服务项
        List<OrderServiceItem> orderServiceItemList = BeanMapper.mapList(quotationOrderWorkAddDIO.getOrderProductDIO().getOrderServiceItemList(), OrderServiceItem.class);
        dto.setOrderServiceItemList(orderServiceItemList);
        // 其他
        BeanMapper.copy(quotationOrderWorkAddDIO, dto);

        // 派单工程师信息
        NewOrderDistributeMasterDTO distributeMasterDTO = BeanMapper.map(quotationOrderWorkAddDIO.getDistributeMasterDIO(), NewOrderDistributeMasterDTO.class);
        dto.setDistributeMasterDTO(distributeMasterDTO);

        // 渠道扩展数据，目前只支持录入 外部渠道单号
        if (Objects.nonNull(quotationOrderWorkAddDIO.getChannelApiExtraData())){
            dto.setOuterId(quotationOrderWorkAddDIO.getChannelApiExtraData().getOuterId());
        }

        // 外部店铺
        if (Objects.nonNull(quotationOrderWorkAddDIO.getExternalShopInfo())) {
            NewOrderExternalShopDTO externalShopDTO = new NewOrderExternalShopDTO();
            externalShopDTO.setExternalShopId(quotationOrderWorkAddDIO.getExternalShopInfo().getExternalShopId());
            externalShopDTO.setInternalShopId(quotationOrderWorkAddDIO.getExternalShopInfo().getInternalShopId());
            externalShopDTO.setInternalShopName(quotationOrderWorkAddDIO.getExternalShopInfo().getInternalShopName());
            externalShopDTO.setInternalBranchShopName(quotationOrderWorkAddDIO.getExternalShopInfo().getInternalBranchShopName());
            dto.setExternalShopInfo(externalShopDTO);
        }

        return super.addOrder(dto);
    }
}
